package com.mvp.shq.washer.ui.login;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.T;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginPresenter, LoginModel> implements LoginConstract.View {


    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.white), 0);

    }
    @Subscribe(code = EventCode.REG_SUCCESS)
    public void resSuccess(){
        finish();
    }

    @OnClick({R.id.tv_forget, R.id.btn_login,R.id.tv_reg})
    public void onViewClicked(View view) {
        String mobile = etUsername.getText().toString();
        String pwd = etPassword.getText().toString();
        switch (view.getId()) {
            case R.id.tv_forget:
                JumpUtil.toForgetPWD(context);
                break;
            case R.id.btn_login:
                if (TextUtils.isEmpty(mobile) || "".equals(mobile)){
                    T.show("请先输入用户名");
                    return;
                }
                if (TextUtils.isEmpty(pwd) || "".equals(pwd)){
                    T.show("请先输入密码");
                    return;
                }
                showLoadingDialog(true);
                presenter.login(mobile,pwd);
                break;
            case R.id.tv_reg:
                JumpUtil.toReg(context);
                break;
        }
    }

    @Override
    public void loginSuccess() {
        T.show("登录成功");
        RxBus.get().send(EventCode.GET_USER_INFO);
        RxBus.get().send(EventCode.UPDATE_JPUSH_ALIAS);
        //JumpUtil.toMain(context);
        finish();
    }

    @Override
    public void loginFail(String msg) {
        T.show(msg);
    }
}
